// 讨论区相关的actions
export default {
  // 获取讨论列表
  async fetchDiscussions({ commit, state, dispatch }, packageName) {
    try {
      commit('SET_LOADING', true)
      dispatch('setLoading', true)
      commit('SET_CURRENT_PACKAGE_NAME', packageName)
      
      const params = {
        page: state.page,
        limit: state.limit,
        ...state.filters
      }
      
      // 调用API获取讨论列表
      const response = await this.app.$api.discussions.getDiscussions(packageName, params)
      
      // 设置讨论列表和分页信息
      commit('SET_DISCUSSIONS', response.data || [])
      commit('SET_PAGINATION', {
        page: response.page || state.page,
        limit: response.limit || state.limit,
        total: response.total || 0
      })
    } catch (error) {
      console.error('Failed to fetch discussions:', error)
      commit('SET_ERROR', error.message)
      this.app.$message.error('获取讨论列表失败')
    } finally {
      commit('SET_LOADING', false)
      dispatch('setLoading', false)
    }
  },
  
  // 获取讨论详情
  async fetchDiscussionDetail({ commit, state, dispatch }, { packageName, discussionId }) {
    try {
      commit('SET_LOADING', true)
      dispatch('setLoading', true)
      commit('SET_CURRENT_PACKAGE_NAME', packageName)
      
      // 调用API获取讨论详情
      const response = await this.app.$api.discussions.getDiscussionDetail(packageName, discussionId)
      
      // 设置讨论详情
      commit('SET_DISCUSSION_DETAIL', response.data)
      
      // 设置回复列表（假设讨论详情中包含回复）
      if (response.data && response.data.replies) {
        commit('SET_REPLIES', response.data.replies)
      }
    } catch (error) {
      console.error('Failed to fetch discussion detail:', error)
      commit('SET_ERROR', error.message)
      this.app.$message.error('获取讨论详情失败')
    } finally {
      commit('SET_LOADING', false)
      dispatch('setLoading', false)
    }
  },
  
  // 创建讨论
  async createDiscussion({ commit, state, dispatch }, discussionData) {
    try {
      commit('SET_SAVING', true)
      dispatch('setLoading', true)
      
      if (!state.currentPackageName) {
        throw new Error('未指定包名称')
      }
      
      // 调用API创建讨论
      const response = await this.app.$api.discussions.createDiscussion(state.currentPackageName, discussionData)
      
      // 创建成功提示
      this.app.$message.success('讨论创建成功')
      
      // 添加新讨论到讨论列表
      if (response.data) {
        commit('ADD_DISCUSSION', response.data)
      }
      
      // 重新获取讨论列表
      await dispatch('fetchDiscussions', state.currentPackageName)
    } catch (error) {
      console.error('Failed to create discussion:', error)
      commit('SET_ERROR', error.message)
      this.app.$message.error('讨论创建失败')
    } finally {
      commit('SET_SAVING', false)
      dispatch('setLoading', false)
    }
  },
  
  // 更新讨论
  async updateDiscussion({ commit, state, dispatch }, { discussionId, discussionData }) {
    try {
      commit('SET_SAVING', true)
      dispatch('setLoading', true)
      
      if (!state.currentPackageName) {
        throw new Error('未指定包名称')
      }
      
      // 调用API更新讨论
      const response = await this.app.$api.discussions.updateDiscussion(state.currentPackageName, discussionId, discussionData)
      
      // 更新成功提示
      this.app.$message.success('讨论更新成功')
      
      // 更新讨论列表和详情
      if (response.data) {
        commit('UPDATE_DISCUSSION', response.data)
      }
    } catch (error) {
      console.error('Failed to update discussion:', error)
      commit('SET_ERROR', error.message)
      this.app.$message.error('讨论更新失败')
    } finally {
      commit('SET_SAVING', false)
      dispatch('setLoading', false)
    }
  },
  
  // 删除讨论
  async deleteDiscussion({ commit, state, dispatch }, discussionId) {
    try {
      commit('SET_DELETING', true)
      dispatch('setLoading', true)
      
      if (!state.currentPackageName) {
        throw new Error('未指定包名称')
      }
      
      // 调用API删除讨论
      await this.app.$api.discussions.deleteDiscussion(state.currentPackageName, discussionId)
      
      // 删除成功提示
      this.app.$message.success('讨论删除成功')
      
      // 从讨论列表中删除
      commit('DELETE_DISCUSSION', discussionId)
    } catch (error) {
      console.error('Failed to delete discussion:', error)
      commit('SET_ERROR', error.message)
      this.app.$message.error('讨论删除失败')
    } finally {
      commit('SET_DELETING', false)
      dispatch('setLoading', false)
    }
  },
  
  // 创建回复
  async createReply({ commit, state, dispatch }, { discussionId, replyData }) {
    try {
      commit('SET_SAVING', true)
      dispatch('setLoading', true)
      
      if (!state.currentPackageName) {
        throw new Error('未指定包名称')
      }
      
      // 调用API创建回复
      const response = await this.app.$api.discussions.createReply(state.currentPackageName, discussionId, replyData)
      
      // 创建成功提示
      this.app.$message.success('回复创建成功')
      
      // 添加新回复到回复列表
      if (response.data) {
        commit('ADD_REPLY', response.data)
      }
    } catch (error) {
      console.error('Failed to create reply:', error)
      commit('SET_ERROR', error.message)
      this.app.$message.error('回复创建失败')
    } finally {
      commit('SET_SAVING', false)
      dispatch('setLoading', false)
    }
  },
  
  // 更改页面
  async changePage({ commit, state, dispatch }, page) {
    commit('SET_PAGINATION', { page })
    await dispatch('fetchDiscussions', state.currentPackageName)
  },
  
  // 更改每页显示数量
  async changeLimit({ commit, state, dispatch }, limit) {
    commit('SET_PAGINATION', { limit, page: 1 })
    await dispatch('fetchDiscussions', state.currentPackageName)
  },
  
  // 更改过滤器
  async changeFilters({ commit, state, dispatch }, filters) {
    commit('SET_FILTERS', filters)
    commit('SET_PAGINATION', { page: 1 })
    await dispatch('fetchDiscussions', state.currentPackageName)
  },
  
  // 清空讨论详情和回复
  clearDiscussionDetail({ commit }) {
    commit('CLEAR_DISCUSSION_DETAIL')
  },
  
  // 清空所有讨论
  clearAllDiscussions({ commit }) {
    commit('CLEAR_ALL_DISCUSSIONS')
  }
}